package games.swingPlus;

import utils.RandomNumber;
import games.Game;
import hardware.Display2D;
import hardware.UI;


public class SwingPlus extends Game 
{

	






private static Storage storage = new Storage(); 
private static Caret caret = new Caret();
private static PlayField playField = new PlayField();
private	int accYRef;
private boolean swing;

private int points;



public SwingPlus()
{
	gameID=2;
	gameName= "SwingPlus";	
	highestSelectableLevel=9;
	
	
	
	
}




public int demo() throws InterruptedException
{
	int cmd;
	do{	
		cmd=displayGameName();
		if (cmd==CMD_NONE)
			{
			cmd=playDemo();
			
			if (cmd==CMD_NONE)
				{
				cmd=displayHighscores();		
				}
			}
		
			if (cmd==CMD_SETUP)
			{
				cmd=configGame();	
			}
	}
	while (cmd==CMD_NONE);
	
return(cmd);		
}





public int configGame() throws InterruptedException
{
	int cmd=CMD_NONE;
	int menue=0;
	
	while (true)
		{
		switch (menue)
			{
			case 0:
				cmd=selectLevel();	
			break;

					
			case 1:
				//cmd=setupAcc();
				break;
			}
	
		if (cmd==CMD_NEXT)
			menue++;

		if (cmd==CMD_PREV)
			menue--;

		
		if (menue<0)
			menue=1;

		if (menue>2)
			menue=0;

		
		if ((cmd==CMD_SELECT) || (cmd==CMD_ABORT))
				return(cmd);
		}	
}






public int playDemo() throws InterruptedException 
{
	prepareLevel(RandomNumber.getRange(4)+1);
		
	int cmd;
	 
	
		Display2D.clear();

		playField.process();
		
		storage.draw();
		caret.draw();
		playField.draw();

		
		Display2D.doRepaint();
		
		cmd=checkKeys();
		if ((cmd==CMD_SELECT) || (cmd==CMD_ABORT) || (cmd==CMD_PREV) || (cmd==CMD_NEXT)  )
			return(cmd);

		Thread.sleep(2000);
		return(CMD_NONE);
}
	



private void prepareLevel(int level) throws InterruptedException
{
	storage.init();
	caret.init();
	playField.init(level);
	accYRef=UI.getAccAxis(UI.ACC_AXIS_Y);
	swing=true;
}






public void play() throws InterruptedException
{
		
	actualLevel=beginLevel;
	points=0;
	
	while(true)
		{
			{
			displayLevelNumber();
			if (playLevel(actualLevel)==true)
				{
				levelWin();
				actualLevel++;
				}
			else
				{
				levelLoose();
				actualLevel=1;
				points=0;
				return;
				}
			}
	
		if (UI.keyLDown())
			return;
		
		}
		
}
		




private void levelWin() throws InterruptedException
{


		
		Display2D.clear();
		
		Display2D.doRepaint();

		Thread.sleep(200);
	

}




private void levelLoose() throws InterruptedException
{
	

	Display2D.clear();
	
	Display2D.doRepaint();

	
	Thread.sleep(10);

	
}



public boolean playLevel(int level) throws InterruptedException
{

		
	prepareLevel(level);
	boolean full=false;
		
	 
		while (true)
		{
			
			Display2D.clear();

			caret.setMoveVector();
			caret.move();

			
			

			if (hardware.UI.keyPressed(UI.KEY_LEFT_DOWN))
			
//			if ((hardware.UI.keyPressed(UI.KEY_LEFT_DOWN)) || swingDetected())
					{
					full=playField.push(caret.load(storage.pop(caret.xPos)),caret.xPos);
					}
			
			points+=playField.process();
			
			storage.draw();
			caret.draw();
			playField.draw();
	
			
			Display2D.doRepaint();
			
			if (full)
				return(true);

			Thread.sleep(10);
		}
		
	}



private boolean swingDetected()
{
	
	int accY;
	accY=UI.getAccAxis(UI.ACC_AXIS_Y);
	
	if ((accY>(accYRef+10)))
		swing=false;
		
	if ((accY<(accYRef+10)) && (swing==false))
		{
		swing=true;
		return(true);
		}
		
	return(false);
}

}

